package cn.wolfcode.luowowo.website.controller;

import cn.wolfcode.luowowo.article.domain.Destination;
import cn.wolfcode.luowowo.article.domain.StrategyCommend;
import cn.wolfcode.luowowo.article.domain.StrategyContent;
import cn.wolfcode.luowowo.article.domain.StrategyDetail;
import cn.wolfcode.luowowo.article.query.StrategyDetailQuery;
import cn.wolfcode.luowowo.article.service.IDestinationService;
import cn.wolfcode.luowowo.article.service.IStrategyCommendService;
import cn.wolfcode.luowowo.article.service.IStrategyDetailService;
import cn.wolfcode.luowowo.article.service.IStrategyTagService;
import cn.wolfcode.luowowo.cache.service.IStrategyStatisVOService;
import cn.wolfcode.luowowo.cache.vo.StrategyStatisVO;
import cn.wolfcode.luowowo.comment.domain.StrategyComment;
import cn.wolfcode.luowowo.comment.query.StrategyCommentQuery;
import cn.wolfcode.luowowo.comment.service.IStrategyCommentService;
import cn.wolfcode.luowowo.common.util.AjaxResult;
import cn.wolfcode.luowowo.member.domain.UserInfo;
import cn.wolfcode.luowowo.search.query.StrategySearchQuery;
import cn.wolfcode.luowowo.search.service.IStrategyEsService;
import cn.wolfcode.luowowo.search.vo.StatisVO;
import cn.wolfcode.luowowo.website.annotion.UserParam;
import com.alibaba.dubbo.config.annotation.Reference;
import com.alibaba.fastjson.JSON;
import com.github.pagehelper.PageInfo;
import org.springframework.data.annotation.Id;
import org.springframework.data.domain.Page;
import org.springframework.stereotype.Controller;
import org.springframework.ui.Model;
import org.springframework.web.bind.annotation.ModelAttribute;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.ResponseBody;

import java.util.List;
import java.util.Map;

@Controller
@RequestMapping("strategy")
public class StrategyController {

    @Reference
    private IStrategyDetailService strategyDetailService;

    @Reference
    private IDestinationService destinationService;

    @Reference
    private IStrategyTagService strategyTagService;

    @Reference
    private IStrategyCommentService strategyCommentService;

    @Reference
    private IStrategyStatisVOService strategyStatisVOService;
    
    @Reference
    private IStrategyCommendService strategyCommendService;

    @Reference
    private IStrategyEsService strategyEsService;

    @RequestMapping("")
    public String index(Model model){
        //commends
        List<StrategyCommend> top5 = strategyCommendService.getTop5();
        model.addAttribute("commends",top5);

        //hotCds
        List<StrategyStatisVO> hotCds =  strategyStatisVOService.queryHotSortTop10();
        model.addAttribute("hotCds",hotCds);

        //abroadCds
        List<StrategyStatisVO> abroadCds =  strategyStatisVOService.queryAbroadCdsSortTop10();
        model.addAttribute("abroadCds",abroadCds);


        //unabroadCds
        List<StrategyStatisVO> unabroadCds =  strategyStatisVOService.queryUnabroadCdsSortTop10();
        model.addAttribute("unabroadCds",unabroadCds);

        //主题推荐
        //themeCds
        List<Map<String,Object>> data = strategyEsService.queryThemeCommend();
        model.addAttribute("themeCds",data);


        //chinas
        List<StatisVO> chinas =  strategyEsService.queryConditionChinas();
        model.addAttribute("chinas",chinas);
        //abroads
        List<StatisVO> abroads =  strategyEsService.queryConditionAbroads();
        model.addAttribute("abroads",abroads);
        //themes
        List<StatisVO> themes =  strategyEsService.queryConditionThemes();
        model.addAttribute("themes",themes);

        return "strategy/index";
    }


    @RequestMapping("/detail")
    public String detail(Model model,Long id){

        StrategyDetail detail = strategyDetailService.get(id);
        StrategyContent content = strategyDetailService.getContent(id);
        detail.setStrategyContent(content);
        model.addAttribute("detail",detail);


        //阅读数
        strategyStatisVOService.increaseViewNum(id,1);
        //共享vo对象
        StrategyStatisVO vo = strategyStatisVOService.getStrategyStatisVo(id);
        model.addAttribute("vo",vo);

        //阅读排行的加分
        strategyStatisVOService.addScoreHot(id,1);
        return "strategy/detail";
    }
    @RequestMapping("/list")
    public String list(Model model, @ModelAttribute("qo")StrategyDetailQuery qo){
        //toasts
        List<Destination> toasts = destinationService.getToasts(qo.getDestId());
        Destination dest = toasts.remove(toasts.size() - 1);
        model.addAttribute("toasts",toasts);
        //dest
        model.addAttribute("dest",dest);
        //tags
        model.addAttribute("tags",strategyTagService.list());

        //pageInfo
        PageInfo pageInfo = strategyDetailService.query(qo);
        model.addAttribute("pageInfo",pageInfo);

        return "strategy/list";
    }

    @RequestMapping("/commentAdd")
    @ResponseBody
    public Object commentAdd(StrategyComment comment, @UserParam UserInfo userInfo){
        comment.setCity(userInfo.getCity());
        comment.setHeadUrl(userInfo.getHeadImgUrl());
        comment.setLevel(userInfo.getLevel());
        comment.setUserId(userInfo.getId());
        comment.setUsername(userInfo.getNickname());

        strategyCommentService.save(comment);

        //评论数添加
        strategyStatisVOService.increaseReplyNum(comment.getDetailId(),1);

        StrategyStatisVO vo = strategyStatisVOService.getStrategyStatisVo(comment.getDetailId());
        //暂时为1

        //评论的加分
        strategyStatisVOService.addScoreHot(comment.getDetailId(),1);
        return AjaxResult.SUCCESS.addData(vo.getReplynum());
    }

    @RequestMapping("/comment")
    public String comment(Model model,@ModelAttribute("qo") StrategyCommentQuery qo){
        Page page = strategyCommentService.query(qo);

        model.addAttribute("page",page);
        return "strategy/commentTpl";
    }
    //1: 当请求过来时，需要判断点赞用户id集合中是否有当前登录用户id
    //2: 如果当前用户不存在集合中，表示点赞操作，点赞树+1,，并将用户id添加到集合中
    //3: 如果当前登录用户存在集合中，则表示取消点赞操作，点赞数-1，并将用户从集合中移除
    @RequestMapping("/commentThumbUp")
    @ResponseBody
    public Object commentThumbUp(String toid,@UserParam UserInfo userInfo){
        strategyCommentService.commentThumbUp(toid,userInfo.getId());
        return AjaxResult.SUCCESS;
    }

    @RequestMapping("/favor")
    @ResponseBody
    public Object favor(Long sid,@UserParam UserInfo userInfo){

        if(userInfo == null){
            //没登录状态
            AjaxResult result = new AjaxResult(false,"请先登录");
            result.setCode(102);//代表未登录
            return result;
        }

        //先处理核心逻辑
        boolean flag = strategyStatisVOService.favor(sid, userInfo.getId());
        StrategyStatisVO vo = strategyStatisVOService.getStrategyStatisVo(sid);

        //收藏加减分
        strategyStatisVOService.addScoreAbroadAndChina(sid,flag?1:-1);


        return new AjaxResult(flag,"").addData(vo);
    }

    @RequestMapping("/strategyThumbup")
    @ResponseBody
    public Object strategyThumbup(Long sid,@UserParam UserInfo userInfo){
        if(userInfo == null){
            //没登录状态
            AjaxResult result = new AjaxResult(false,"请先登录");
            result.setCode(102);//代表未登录
            return result;
        }
        //今天顶过了
        boolean flag = strategyStatisVOService.thumbup(sid, userInfo.getId());
        StrategyStatisVO vo = strategyStatisVOService.getStrategyStatisVo(sid);

        //点赞：国内外排行的加分
        if(flag){
            strategyStatisVOService.addScoreAbroadAndChina(sid,1);
        }
        return new AjaxResult(flag,"").addData(vo);

    }

    @RequestMapping("/searchPage")
    public String searchPage(Model model,@ModelAttribute("qo")StrategySearchQuery qo){

        Page page = strategyEsService.query(qo);
        model.addAttribute("page",page);
        return "strategy/searchPageTpl";
    }

}
